// pages/weather/weather.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityName: '定位中',
    temperature: '25',
    todayWeather: '晴',
    day1Weather: [],
    day1WeatherImg: [],
    day2Weather: [],
    day2WeatherImg: [],
    day3Weather: [],
    day3WeatherImg: [],
    slogan: '美好的事总会发生',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 发送请求从高德地图天气API获取数据
    wx.request({
      url: 'https://restapi.amap.com/v3/weather/weatherInfo?key=5543b2ff57df5adcde39b35beda5d570&city=都江堰&extensions=all',
      success: (res) => {
        console.log(res);
        console.log(res.data.forecasts[0].city);
        console.log(res.data.forecasts[0].casts);
        this.setData({
          cityName: res.data.forecasts[0].city,
          day1Weather: res.data.forecasts[0].casts[0],
          day1WeatherImg: weatherImgFn(0),
          day2WeatherImg: weatherImgFn(1),
          day3WeatherImg: weatherImgFn(2),
          day2Weather: res.data.forecasts[0].casts[1],
          day3Weather: res.data.forecasts[0].casts[2],
          temperature: res.data.forecasts[0].casts[0].daytemp,
          todayWeather: res.data.forecasts[0].casts[0].dayweather,
          slogan: sloganFn(res.data.forecasts[0].casts[0].dayweather)
        })

        // 判断并修改天气图标
        function weatherImgFn(number) {
          var weatherData = res.data.forecasts[0].casts[number].dayweather;
          if (weatherData == "晴") {
            return "../../img/weather_icon/sun.png";
          };
          if (weatherData == "阴") {
            return "../../img/weather_icon/overcast.png";
          };
          if (weatherData == "阵雨" || weatherData == "小雨" || weatherData == "中雨" || weatherData == "小雨-中雨") {
            return "../../img/weather_icon/rain_1.png";
          };
          if (weatherData == "大雨" || weatherData == "暴雨" || weatherData == "大暴雨" || weatherData ==
            "特大暴雨" || weatherData == "极端降雨" || weatherData == "中雨-大雨" || weatherData == "大雨-暴雨" ||
            weatherData ==
            "暴雨-大暴雨" || weatherData == "大暴雨-特大暴雨") {
            return "../../img/weather_icon/rain_2.png";
          };
          if (weatherData == "多云" || weatherData == "晴间多云" || weatherData == "少云") {
            return "../../img/weather_icon/cloudy.png";
          };
          if (weatherData == "雷阵雨" || weatherData == "雷阵雨并伴有冰雹") {
            return "../../img/weather_icon/storm_1.png";
          };
          if (weatherData == "强雷阵雨") {
            return "../../img/weather_icon/storm_2.png";
          };
          if (weatherData == "雪" || weatherData == "阵雪" || weatherData == "小雪" || weatherData == "中雪") {
            return "../../img/weather_icon/snow_1.png";
          };
          if (weatherData == "大雪" || weatherData == "暴雪") {
            return "../../img/weather_icon/snow_2.png";
          };
          if (weatherData == "雨雪天气" || weatherData == "雨夹雪" || weatherData == "阵雨夹雪") {
            return "../../img/weather_icon/snow_rain.png";
          };
        }
        // slogan替换函数
        function sloganFn(data) {
          if (data == "晴") {
            return "天气真好,不出去走走吗"
          };
          if (data == "阴") {
            return "太阳暂时藏起来了"
          };
          if (data == "阵雨" || data == "小雨" || data == "中雨") {
            return "外面在下雨,出门记得带伞哦"
          };
          if (data == "大雨" || data == "暴雨" || data == "大暴雨" || data ==
            "特大暴雨") {
            return "外面的雨真大,还是在家休息吧"
          };
          if (data == "多云" || data == "晴间多云" || data == "少云") {
            return "天蓝蓝云漫漫,太阳藏在云里边"
          };
          if (data == "雷阵雨") {
            return "雷阵雨!最好不要出门哦"
          };
          if (data == "强雷阵雨") {
            return "雷阵雨!最好不要出门哦"
          };
          if (data == "雪" || data == "阵雪" || data == "小雪" || data == "中雪") {
            return "下雪了,注意保暖"
          };
          if (data == "大雪" || data == "暴雪") {
            return "下大雪了,还是待在家取暖吧"
          };
          if (data == "雨雪天气" || data == "雨夹雪" || data == "阵雨夹雪") {
            return "雨夹雪,出门记得带伞,注意保暖"
          }
        }
      }
    })
  },

  // 手动输入获取城市名
  getCityName() {
    let that = this
    wx.showModal({
      title: '提示',
      editable: true,
      placeholderText: '请输入您的城市名',
      success(res) {
        if (res.confirm) {
          console.log(res.content);
          // 发送请求从高德地图天气API获取数据
          wx.request({
            url: 'https://restapi.amap.com/v3/weather/weatherInfo?key=5543b2ff57df5adcde39b35beda5d570&city=' + res.content + '&extensions=all',
            success: (res) => {
              console.log(res);
              console.log(res.data.forecasts[0].city);
              console.log(res.data.forecasts[0].casts);
              that.setData({
                cityName: res.data.forecasts[0].city,
                day1Weather: res.data.forecasts[0].casts[0],
                day1WeatherImg: weatherImgFn(0),
                day2WeatherImg: weatherImgFn(1),
                day3WeatherImg: weatherImgFn(2),
                day2Weather: res.data.forecasts[0].casts[1],
                day3Weather: res.data.forecasts[0].casts[2],
                temperature: res.data.forecasts[0].casts[0].daytemp,
                todayWeather: res.data.forecasts[0].casts[0].dayweather,
                slogan: sloganFn(res.data.forecasts[0].casts[0].dayweather)
              })

              // 判断并修改天气图标
              function weatherImgFn(number) {
                var weatherData = res.data.forecasts[0].casts[number].dayweather;
                if (weatherData == "晴") {
                  return "../../img/weather_icon/sun.png";
                };
                if (weatherData == "阴") {
                  return "../../img/weather_icon/overcast.png";
                };
                if (weatherData == "阵雨" || weatherData == "小雨" || weatherData == "中雨" || weatherData == "小雨-中雨") {
                  return "../../img/weather_icon/rain_1.png";
                };
                if (weatherData == "大雨" || weatherData == "暴雨" || weatherData == "大暴雨" || weatherData ==
                  "特大暴雨" || weatherData == "极端降雨" || weatherData == "中雨-大雨" || weatherData == "大雨-暴雨" ||
                  weatherData ==
                  "暴雨-大暴雨" || weatherData == "大暴雨-特大暴雨") {
                  return "../../img/weather_icon/rain_2.png";
                };
                if (weatherData == "多云" || weatherData == "晴间多云" || weatherData == "少云") {
                  return "../../img/weather_icon/cloudy.png";
                };
                if (weatherData == "雷阵雨" || weatherData == "雷阵雨并伴有冰雹") {
                  return "../../img/weather_icon/storm_1.png";
                };
                if (weatherData == "强雷阵雨") {
                  return "../../img/weather_icon/storm_2.png";
                };
                if (weatherData == "雪" || weatherData == "阵雪" || weatherData == "小雪" || weatherData == "中雪") {
                  return "../../img/weather_icon/snow_1.png";
                };
                if (weatherData == "大雪" || weatherData == "暴雪") {
                  return "../../img/weather_icon/snow_2.png";
                };
                if (weatherData == "雨雪天气" || weatherData == "雨夹雪" || weatherData == "阵雨夹雪") {
                  return "../../img/weather_icon/snow_rain.png";
                };
              }
              // slogan替换函数
              function sloganFn(data) {
                if (data == "晴") {
                  return "天气真好,不出去走走吗"
                };
                if (data == "阴") {
                  return "太阳暂时藏起来了"
                };
                if (data == "阵雨" || data == "小雨" || data == "中雨") {
                  return "外面在下雨,出门记得带伞哦"
                };
                if (data == "大雨" || data == "暴雨" || data == "大暴雨" || data ==
                  "特大暴雨") {
                  return "外面的雨真大,还是在家休息吧"
                };
                if (data == "多云" || data == "晴间多云" || data == "少云") {
                  return "天蓝蓝云漫漫,太阳藏在云里边"
                };
                if (data == "雷阵雨") {
                  return "雷阵雨!最好不要出门哦"
                };
                if (data == "强雷阵雨") {
                  return "雷阵雨!最好不要出门哦"
                };
                if (data == "雪" || data == "阵雪" || data == "小雪" || data == "中雪") {
                  return "下雪了,注意保暖"
                };
                if (data == "大雪" || data == "暴雪") {
                  return "下大雪了,还是待在家取暖吧"
                };
                if (data == "雨雪天气" || data == "雨夹雪" || data == "阵雨夹雪") {
                  return "雨夹雪,出门记得带伞,注意保暖"
                }
              }
            }
          })
        }
      }
    })
  },

  toCamera() {
    wx.navigateTo({
      url: '../camera/camera',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})